package com.h5lib.nchuoffice.app.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.h5lib.server.Interceptor;

import fi.iki.elonen.NanoHTTPD.IHTTPSession;

/**
 * 
 * @author framer
 *
 */
public class AuthInterceptor implements Interceptor{
	
	/**
	 * 权限认证Key
	 */
	public static String AUTH_KEY="_USER_AUTH_KEY_";
	
	private static String authValue = null;
	
	public static List<String> expUrl = new ArrayList<String>();
	static{
		expUrl.add("/login.html");
		expUrl.add("/logout.html");
		expUrl.add("/doLogin");
		expUrl.add("/doHybridLogin");
		expUrl.add("/doOffLogin");
	}
	
	@Override
	public boolean intercept(IHTTPSession session) {
		if(expUrl.contains(session.getUri())){
			return true;
		}
		if(getAuthValue().equals(session.getCookies().read("_USER_AUTH_KEY_"))){
			return true;
		}
		else{
			return false;
		}
	}
	
	@Override
	public Object handleError(IHTTPSession session) {
		
		return "/ui/login.html";
	}
	
	/**
	 * 获取权限
	 * @return
	 */
	public static String getAuthValue() {
		if(authValue == null){
			authValue =  UUID.randomUUID().toString();
		}
		return authValue;
	}

}
